/*============================================================================
  File:      DataValues.cpp
 
  Summary:   Implementation of functions used to simplify representation of data
			 values.

  Date:		 June 30, 2004
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.
 
  Copyright (C) 2003 Microsoft Corporation.  All rights reserved.
 
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.
 
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
============================================================================*/

#include "stdafx.h"
#include "DataValues.h" 

// Continuous variables are converted in the obvious way:

DBL	DblContinuous(DM_STATE_VALUE& dmstatevalue)
{
	if (dmstatevalue.si.iFlag == (ULONG) 0xFFF80000L)
	{
		return dblMissing;
	}
	else
	{
		return dmstatevalue.r8;
	}
}

DM_STATE_VALUE	DmstatevalueContinuous(DBL dbl)
{
	DM_STATE_VALUE dmstatevalue;

	if (dbl == dblMissing)
	{
		dmstatevalue.si.iFlag = (ULONG) 0xFFF80000L;
	}
	else
	{
		dmstatevalue.si.iFlag	= 0;
		dmstatevalue.r8			= dbl;
	}

	return dmstatevalue;
}

//
// There are two flavors of categorical-variable transformation:
//
// (1) Missing-at-random categorical variables
//
//		For this transformation, all state 0 values from AS are converted to
//		dblMissing, and all other state values are decremented by one. This
//		leads to a categorical variable with states that are zero based.

DBL	DblCategoricalMAR(DM_STATE_VALUE& dmstateval)
{
	if (dmstateval.si.iState == 0)
	{
		return dblMissing;
	}
	else
	{
		return (DBL) dmstateval.si.iState - 1;
	}
}

DM_STATE_VALUE	DmstatevalueCategoricalMAR(DBL dbl)
{
	DM_STATE_VALUE dmstatevalue;

	dmstatevalue.si.iFlag	= 0xFFF80001L; // Another magic value: its categorical
	dmstatevalue.si.iState	= (dbl == dblMissing) ? 0 : (ULONG) dbl + 1;

	return dmstatevalue;
}



// (2) Not-missing-at-random categorical variable
//
//		For this transformation, the states of categorical variables are
//		left as-is.



DBL	DblCategorical(DM_STATE_VALUE& dmstatevalue)
{
	return (DBL) dmstatevalue.si.iState;
}

DM_STATE_VALUE	DmstatevalueCategorical(DBL dbl)
{
	assert(dbl != dblMissing);

	DM_STATE_VALUE dmstatevalue;

	dmstatevalue.si.iFlag		= 0;
	dmstatevalue.si.iState	= (ULONG) dbl;

	return dmstatevalue;
}

